package com.tailyou.hms.firstapp.adapter;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.tailyou.hms.firstapp.R;
import com.tailyou.hms.firstapp.bean.MLBean;

import java.util.List;

public abstract class MenuAdapter extends RecyclerView.Adapter<MenuAdapter.RVHolder> {

    private List<MLBean.DataDTO> datas;
    private int selectedPosition = 0;

    public MenuAdapter(List<MLBean.DataDTO> datas) {
        this.datas = datas;
    }

    @NonNull
    @Override
    public MenuAdapter.RVHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout_menu, parent, false);
        return new RVHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MenuAdapter.RVHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.tvName.setBackgroundColor(position == selectedPosition ? Color.rgb(8, 217, 214) : Color.TRANSPARENT);
        holder.tvName.setTextColor(position == selectedPosition ? Color.WHITE : Color.GRAY);
        holder.tvName.setText(datas.get(position).getCatgoryname());
        holder.tvName.setOnClickListener(v -> {
            selectedPosition = position;
            notifyDataSetChanged();
            clickItem(position);
        });
    }

    public abstract void clickItem(int position);

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public class RVHolder extends RecyclerView.ViewHolder {
        private TextView tvName;

        public RVHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tvName);
        }
    }
}
